package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatGoodsCategory;
import com.happy.cat.project.system.service.ICatGoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品分类Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/goodsCategory" )
@Api("商品分类 API")
public class CatGoodsCategoryController extends BaseController {

    private final ICatGoodsCategoryService iCatGoodsCategoryService;

    @GetMapping(value = "/all" )
    public AjaxResult all() {
        return AjaxResult.success(iCatGoodsCategoryService.list());
    }


    /**
     * 查询商品分类列表
     */
    @ApiOperation(value = "查询商品分类列表",notes = "查询商品分类列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatGoodsCategory catGoodsCategory)
    {
        startPage();
        LambdaQueryWrapper<CatGoodsCategory> lqw = new LambdaQueryWrapper<CatGoodsCategory>();
        if (StringUtils.isNotBlank(catGoodsCategory.getName())){
            lqw.like(CatGoodsCategory::getName ,catGoodsCategory.getName());
        }
        if (catGoodsCategory.getCreateDate() != null){
            lqw.eq(CatGoodsCategory::getCreateDate ,catGoodsCategory.getCreateDate());
        }
        if (catGoodsCategory.getStatus() != null){
            lqw.eq(CatGoodsCategory::getStatus ,catGoodsCategory.getStatus());
        }
        List<CatGoodsCategory> list = iCatGoodsCategoryService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @ApiOperation(value = "导出商品分类列表",notes = "导出商品分类列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:export')" )
    @Log(title = "商品分类" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatGoodsCategory catGoodsCategory) {
        LambdaQueryWrapper<CatGoodsCategory> lqw = new LambdaQueryWrapper<CatGoodsCategory>(catGoodsCategory);
        List<CatGoodsCategory> list = iCatGoodsCategoryService.list(lqw);
        ExcelUtil<CatGoodsCategory> util = new ExcelUtil<CatGoodsCategory>(CatGoodsCategory. class);
        return util.exportExcel(list, "goodsCategory" );
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation(value = "获取商品分类详细信息",notes = "获取商品分类详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatGoodsCategoryService.getById(id));
    }

    /**
     * 新增商品分类
     */
    @ApiOperation(value = "新增商品分类",notes = "新增商品分类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:add')" )
    @Log(title = "商品分类" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatGoodsCategory catGoodsCategory) {
        catGoodsCategory.setCreateDate(new Date());
        catGoodsCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGoodsCategoryService.save(catGoodsCategory) ? 1 : 0);
    }

    /**
     * 修改商品分类
     */
    @ApiOperation(value = "修改商品分类",notes = "修改商品分类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:edit')" )
    @Log(title = "商品分类" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatGoodsCategory catGoodsCategory) {
        catGoodsCategory.setCreateDate(new Date());
        catGoodsCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGoodsCategoryService.updateById(catGoodsCategory) ? 1 : 0);
    }

    /**
     * 删除商品分类
     */
    @ApiOperation(value = "删除商品分类",notes = "删除商品分类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:remove')" )
    @Log(title = "商品分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatGoodsCategoryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
